package impl;

import java.math.BigDecimal;

import api.InvalidAuthenticationException;
import api.UserProfileTypes.Gender;
import api.UserProfileTypes.NotificationApproach;
import api.ejb.IUserDataManager;

import javax.ejb.Stateless;
import javax.ejb.TransactionAttribute;
import javax.ejb.TransactionAttributeType;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;


import org.jboss.logging.Logger;

import p3_entities.Userphoto;
import p3_entities.Userprofile;


/**
 * Session Bean implementation class UserDataManager
 */
@Stateless(name = "IUserDataManager")
//@TransactionAttribute(value = TransactionAttributeType.REQUIRES_NEW)
public class UserDataManager implements IUserDataManager {
	@PersistenceContext(unitName = "PhaseBookPU")
	EntityManager em;

	private static final Logger log = Logger.getLogger(UserDataManager.class);

	/**
	 * Default constructor.
	 */
	public UserDataManager() {
		// TODO Auto-generated constructor stub
	}

	private Userprofile getUserProfile(int uid) {

		return (Userprofile) em.find(Userprofile.class, uid);

	}

	@Override
	public boolean updateName(int uid, String name) {
		
		log.info("Executing ---------->");

		Userprofile userProfile = getUserProfile(uid);

		if (userProfile == null) {
			log.warn("User not found");
			return false;
		}

		userProfile.setName(name);

		em.persist(userProfile);
		log.info("<--------------- Done");


		return true;
	}

	@Override
	public boolean updateAge(int uid, int age) {

		log.info("Executing ---------->");


		Userprofile userProfile = getUserProfile(uid);

		if (userProfile == null) {
			log.warn("User not found");
			return false;
		}

		userProfile.setAge(new BigDecimal(age));

		em.persist(userProfile);
		log.info("<--------------- Done");


		return true;
	}

	@Override
	public boolean updateCity(int uid, String city) {

		log.info("Executing ---------->");


		Userprofile userProfile = getUserProfile(uid);

		if (userProfile == null) {
			log.warn("User not found");
			return false;
		}

		userProfile.setLocality(city);

		em.persist(userProfile);
		log.info("<--------------- Done");

		return true;
	}

	@Override
	public boolean updateGender(int uid, Gender sex) {

		log.info("Executing ---------->");


		Userprofile userProfile = getUserProfile(uid);

		if (userProfile == null) {
			log.warn("User not found");
			return false;
		}

		userProfile.setGender(sex.equals(Gender.FEMALE) ? Gender.FEMALE.name()
				: Gender.MALE.name());

		em.persist(userProfile);
		log.info("<--------------- Done");

		return true;
	}

	@Override
	public boolean updateEmail(int uid, String email) {

		log.info("Executing ---------->");


		Userprofile userProfile = getUserProfile(uid);

		if (userProfile == null) {
			log.warn("User not found");
			return false;
		}

		userProfile.setEmail(email);

		em.persist(userProfile);
		
		log.info("<--------------- Done");


		return true;
	}

	@Override
	public boolean updateNotificationApproach(int uid,
			NotificationApproach approach, boolean flag) {
		

		log.info("Executing ---------->");


		Userprofile userProfile = getUserProfile(uid);

		if (userProfile == null) {
			log.warn("User not found");
			return false;
		}

		if (approach.equals(NotificationApproach.ALL))
			userProfile.setNotify_OnChange(flag);

		if (approach.equals(NotificationApproach.DIGEST))
			userProfile.setNotify_Digest(flag);

		em.persist(userProfile);
		
		log.info("<--------------- Done");


		return true;
	}

	@Override
	public Userprofile getProfileById(int uid) {
		return getUserProfile(uid);
	}

	@Override
	public Userprofile getProfileByUsername(String username) {
		log.info("Executing ---------->");

		Query query = em.createNamedQuery("getUserProfile");
		query.setParameter("email", username);
		
		Userprofile u = (Userprofile) query.getSingleResult();

		
		log.info("<--------------- Done");
		
		return u;



	}

	@Override
	public boolean updatePhoto(int uid, int photoId) {
		
		log.info("Executing ---------->");

		Userprofile userProfile = getUserProfile(uid);
		Userphoto userPhoto = (Userphoto) em.find(Userphoto.class, photoId);

		if ((userProfile == null) || (userPhoto == null)) {
			System.out.println("*** Failed to update User Photo ***");
			return false;
		}

		userProfile.setPhotoid(userPhoto);

		em.persist(userProfile);
		
		log.info("<--------------- Done");


		return true;
	}

	@Override
	public String getEMailById(Integer uid) throws InvalidAuthenticationException {
		
		return this.getProfileById(uid).getEmail();
	}

}
